
package cn.winwintech.Incense.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.webapp.respm.BaseResp;
import com.base.webapp.respm.ResException;

import cn.winwintech.Incense.client.request.DownloadReq;
import cn.winwintech.Incense.client.request.LoginReq;
import cn.winwintech.Incense.client.request.OperationReq;
import cn.winwintech.Incense.client.request.OperationReq.OperationBean;
import cn.winwintech.Incense.client.request.TokenReq;
import cn.winwintech.Incense.service.UserService;

@Controller
public class UserController {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private UserService service;

	/**
	 * @param regist
	 * @param result
	 * @return 注册
	 */

	/**
	 * @param login
	 * @param result
	 * @return 登录
	 */
	@RequestMapping(value = "/user/login")
	public @ResponseBody BaseResp login(@Valid LoginReq login, BindingResult result) {
		BaseResp resp = new BaseResp();
		if (resp.processResultValid(result)) {
			switch (service.checkUser(login.getPhone(), login.getPassword())) {
			case UserService.USER_NOT_EXIST:
				resp.userNotExist();
				break;
			case UserService.PASSWORD_ERROR:
				resp.userPasswordError();
				break;
			}
			if (resp.isOK()) {
				resp.setData(service.refreshTokenByPhone(login.getPhone()));
			}
		}
		return resp;
	}

	@RequestMapping(value = "/user/synchronize_upload", produces = "application/json")
	public @ResponseBody BaseResp upload(@Valid OperationReq req, BindingResult result)
			throws UnsupportedEncodingException {
		BaseResp resp = new BaseResp();
		String token = request.getHeader("token");
		if (token == null) {
			token = req.getToken();
		}
		if (resp.processResultValid(result)) {
			OperationBean operations = new OperationBean();
			try {
				operations = req.toObject();
				if (operations.getTreeId() == null) {
					resp.paramError();
				}
			} catch (Exception e) {
				resp.paramError();
				return resp;
			}
			try {
				if (service.synchronizeUpload(operations, token, req.getPhoto1(), req.getPhoto2(), req.getPhoto3(),
						req.getPhoto4()))
				{
					resp.ok();
				}
			} catch (ResException e) {
				resp.bindException(e);
			} catch (IllegalStateException e) {
				resp.imageNotExists(e);
			} catch (IOException e) {
				resp.imageIOError(e);
			}
		}
		return resp;

	}

	@RequestMapping(value = "/user/synchronize_download")
	public @ResponseBody BaseResp downLoad(@Valid DownloadReq req, BindingResult result) {
		BaseResp resp = new BaseResp();
		if (resp.processResultValid(result)) {
			resp.setData(service.download(req.getPage(), req.getMaxUpdateAt()));
		}
		return resp;

	}

	/**
	 * @param token
	 * @param result
	 * @return 自动登录 登录以后打开APP的时候
	 */
	@RequestMapping(value = "/user/login_auto")
	public @ResponseBody BaseResp autologin(@Valid TokenReq token, BindingResult result) {
		BaseResp resp = new BaseResp();
		if (resp.processResultValid(result)) {
			try {
				resp.setData(service.refreshTokenByToken(token.getToken()));
			} catch (ResException e) {
				resp.bindException(e);
			}
		}
		return resp;
	}

}
